package com.example.cameraxdemo.activity

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.content.ContentValues
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.ImageView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import com.example.cameraxdemo.R
import com.example.cameraxdemo.utils.Constants.Companion.REQUEST_CODE_CAMERA
import com.example.cameraxdemo.utils.Constants.Companion.REQUEST_CODE_CROP
import com.example.cameraxdemo.utils.FileManager
import com.example.cameraxdemo.utils.FileUtil
import com.google.android.material.internal.EdgeToEdgeUtils
import java.io.File

/**
 *@author: njb
 *@date:   2023/8/15 17:20
 *@desc:
 */
class CameraActivity :AppCompatActivity(){
    private var mUploadImageUri: Uri? = null
    private var mUploadImageFile: File? = null
    private var photoUri: Uri? = null
    private val btnCamera:Button by lazy { findViewById(R.id.btnCamera) }
    private val ivAvatar:ImageView by lazy { findViewById(R.id.iv_avatar) }
    private val TAG = CameraActivity::class.java.name
    private val rootView : View by lazy {findViewById(R.id.camera_root) }

    @SuppressLint("RestrictedApi")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EdgeToEdgeUtils.applyEdgeToEdge(this.window,true)
        setContentView(R.layout.activity_camera)
        initView()
        ViewCompat.setOnApplyWindowInsetsListener(rootView) { v, insets ->
            val stateBars = insets.getInsets(WindowInsetsCompat.Type.statusBars())
            v.setPadding(stateBars.left, stateBars.top, stateBars.right, stateBars.bottom)
            insets
        }
    }

    private fun initView() {
        btnCamera.setOnClickListener {
            startSystemCamera()
        }
    }

    /**
     * 调起系统相机拍照
     */
    private fun startSystemCamera() {
        val takeIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        val values = ContentValues()
        //根据uri查询图片地址
        photoUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
        LogUtils.d(TAG, "photoUri:" + photoUri?.authority + ",photoUri:" + photoUri?.path)
        //放入拍照后的地址
        takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
        //调起拍照
        startActivityForResult(
            takeIntent,
            REQUEST_CODE_CAMERA
        )
    }


    /**
     * 设置用户头像
     */
    private fun setAvatar() {
        val file: File? = if (mUploadImageUri != null) {
            FileManager.getMediaUri2File(mUploadImageUri)
        } else {
            mUploadImageFile
        }
        Glide.with(this).load(file).into(ivAvatar)
        LogUtils.d(TAG,"filepath"+ file!!.absolutePath)
    }

    /**
     * 系统裁剪方法
     */
    private fun workCropFun(imgPathUri: Uri?) {
        mUploadImageUri = null
        mUploadImageFile = null
        if (imgPathUri != null) {
            val imageObject: Any = FileUtil.getHeadJpgFile()
            if (imageObject is Uri) {
                mUploadImageUri = imageObject
            }
            if (imageObject is File) {
                mUploadImageFile = imageObject
            }
            val intent = Intent("com.android.camera.action.CROP")
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            }
            intent.run {
                setDataAndType(imgPathUri, "image/*")// 图片资源
                putExtra("crop", "true") // 裁剪
                putExtra("aspectX", 1) // 宽度比
                putExtra("aspectY", 1) // 高度比
                putExtra("outputX", 150) // 裁剪框宽度
                putExtra("outputY", 150) // 裁剪框高度
                putExtra("scale", true) // 缩放
                putExtra("return-data", false) // true-返回缩略图-data，false-不返回-需要通过Uri
                putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()) // 保存的图片格式
                putExtra("noFaceDetection", true) // 取消人脸识别
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                    putExtra(MediaStore.EXTRA_OUTPUT, mUploadImageUri)
                } else {
                    val imgCropUri = Uri.fromFile(mUploadImageFile)
                    putExtra(MediaStore.EXTRA_OUTPUT, imgCropUri)
                }
            }
            startActivityForResult(
                intent, REQUEST_CODE_CROP
            )
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_CAMERA) {//拍照回调
                workCropFun(photoUri)
            } else if (requestCode == REQUEST_CODE_CROP) {//裁剪回调
                setAvatar()
            }
        }
    }
}